package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



public class roomtype_List extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public roomtype_List() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//根据条件返回房型信息
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();
		JSONArray jsonarray = new JSONArray();
		JSONObject jsonobj = new JSONObject();


		conn cn = new conn();
	
	    String f=request.getParameter("f");

		ResultSet rs=null;

		if(f.equals("1"))
		{
			String d1=request.getParameter("d1");
			String d2=request.getParameter("d2");
			rs=cn.query("select a.*,ifnull(tt,0) as tt from  roomtype a left join (" +
					"select tid, count(*) as tt \n" +
					"from rooms\n" +
					"where rno not in  (select rno\n" +
					"              from ordersmore a\n" +
					"                       left join orders o on a.ono = o.ono\n" +
					"              where oflag <> '已完成' and ( (sdate>='"+d1+"' and sdate<='"+d1+"') or (edate>='"+d2+"' and edate<='"+d2+"')))\n" +
					"group by tid" +
					") b on a.rid=b.tid order by rid  ");
		}
		else if(f.equals("2"))
		{
			String id = request.getParameter("id");
			rs=cn.query("select * from  roomtype where rid="+id);
		}

		while (true)
		{
			try {
				if (!rs.next()) break;
             jsonobj.put("rid", rs.getString("rid"));
             jsonobj.put("rname", rs.getString("rname"));
             jsonobj.put("rpic", rs.getString("rpic"));
             jsonobj.put("rpice", rs.getString("rpice"));
             jsonobj.put("rmemo", rs.getString("rmemo"));

             if(f.equals("1"))
			 {
				 jsonobj.put("tt", rs.getString("tt"));
			 }
		
				jsonarray.add(jsonobj);

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		out.println(jsonarray);	
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

